package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBeautyData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserBeautyDataDaoImpl extends TempDataObjectDao<UserBeautyData> {

    public UserBeautyData getUserBeautyData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_beauty_detail where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBeautyData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserBeautyData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_beauty_detail(userId,energy,lastRestoreTime,noMakeChildTimes,totalFateSkillUpgradeTimes) values(?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getEnergy());
            call.setLong(i++, data.getLastRestoreTime());
            call.setInt(i++, data.getNoMakeChildTimes());
            call.setLong(i++, data.getTotalFateSkillUpgradeTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBeautyData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_beauty_detail SET energy=?,lastRestoreTime=?,noMakeChildTimes=?,totalFateSkillUpgradeTimes=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getEnergy());
            call.setLong(i++, data.getLastRestoreTime());
            call.setInt(i++, data.getNoMakeChildTimes());
            call.setLong(i++, data.getTotalFateSkillUpgradeTimes());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBeautyData parseData(ResultSet rs) throws SQLException {
        UserBeautyData data = new UserBeautyData();
        data.setUserId(rs.getLong("userId"));
        data.setEnergy(rs.getInt("energy"));
        data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
        data.setNoMakeChildTimes(rs.getInt("noMakeChildTimes"));
        data.setTotalFateSkillUpgradeTimes(rs.getLong("totalFateSkillUpgradeTimes"));
        return data;
    }
}
